<template>
  <div class="menu-box" :class="{ nosee: !show }">
    <h1>COOL-ADMIN</h1>
    <menusList v-model:show="show" :tree="menusStore.showTree"></menusList>
  </div>

  <div class="shade" v-if="show" @click.self="hiddenMenu"></div>

  <div class="content">
    <div class="header">
      <i class="iconfont" @click="showed" v-if="show">&#xe622;</i>
      <i class="iconfont" @click="showed" v-if="!show">&#xe790;</i>
      <breadCru></breadCru>
      <el-dropdown
        class="user"
        trigger="click"
        hide-on-click
        @command="onCommand"
      >
        <div class="user">
          <p>{{ userStore.userInfo.nickName }}</p>
          <el-avatar size="small" :src="userStore.userInfo.headImg" />
        </div>

        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="my">
              <i class="cl-iconfont cl-icon-user"></i>
              <span>个人中心</span>
            </el-dropdown-item>
            <el-dropdown-item command="exit">
              <i class="cl-iconfont cl-icon-exit"></i>
              <span>退出</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>

    <div class="showCon">
      <RouterView></RouterView>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getInfoApi, logoutApi } from "@/api"
import resizeHooks from "@/hooks/resizeHooks"
import breadCru from "@/components/breadCru.vue"

// 引入仓库
import useUser from "@/stores/user"
import menus from "@/stores/menus"
import menusList from "@/components/menus/menus-list.vue"
import store from "store2"
import router from "@/router"
import { ElMessage } from "element-plus"
const menusStore = menus()
const userStore = useUser()

// 侧边栏收缩
// 使用组合式函数
const show = resizeHooks()

// 点击遮罩层收缩侧边栏
function hiddenMenu() {
  show.value = false
}

// 收缩侧边栏
function showed() {
  show.value = !show.value
}

// 获取用户信息
getInfo()

// 下拉框点击事件
async function onCommand(name: string) {
  switch (name) {
    case "my":
      // 个人中心,跳转到个人中心路由
      // router.push("/my/info")
      break
    // 退出登录
    case "exit":
      // 跳转到登录路由
      router.push({ name: "login", replace: true })
      await logoutApi()
      // 退出登录
      // 清空本地信息
      store.clear()
      ElMessage({
        showClose: true,
        message: "已退出登录",
        type: "success"
      })
      break
  }
}

async function getInfo() {
  // 获取用户信息
  const user = await getInfoApi()
  userStore.userInfo = user
}
</script>

<style lang="scss" scoped>
.menu-box {
  background: var(--primary-color);
  height: 100%;
  overflow-x: auto;
  width: 250px;
  transition: all 0.5s;
  h1 {
    color: #fff;
    font-weight: bold;
    font-size: 26px;
    white-space: nowrap;
    text-align: center;
    line-height: 80px;
  }

  @media (max-width: 800px) {
    position: fixed;
    background: var(--primary-color);
    z-index: 20;
  }
}

@media (max-width: 800px) {
  .shade {
    background-color: #00000080;
    position: fixed;
    z-index: 10;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
  }
}

.nosee {
  width: 0;
}

.content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  .header {
    height: 50px;
    width: 100%;
    background-color: #fff;
    padding: 0 10px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .iconfont {
      color: black;
      cursor: pointer;
      font-size: 20px;
    }

    .user {
      color: #606266;
      margin-left: auto;
      display: flex;
      align-items: center;
      cursor: pointer;
      p {
        margin-right: 15px;
      }
    }
  }

  .showCon {
    flex: 1;
    overflow: hidden;
    padding: 10px;
    box-sizing: border-box;
    width: 100%;
  }
}

::-webkit-scrollbar {
  display: none;
}
</style>
